
clear all;
close all;
clc

cd  E:\ReplicateBuild\02_code\03_matlab
%%
% part 0; baseline

[display_var_baseline] = robustness_cf_dist('baseline');

% part 1: class size
[display_var_constant] = robustness_cf_dist('constantalt');
% CFR, constant class size 
[display_var_CFRconstant] = robustness_cf_dist('cfrCalt');

display_var_CFRconstant=display_var_CFRconstant-display_var_CFRconstant(1,1) + display_var_baseline(1,1);


% part 2: vary choice set construction for teachers

[display_var_teach_buff7] = robustness_cf_dist('teach_buff7');
[display_var_teach_day1] = robustness_cf_dist('teach_day1');
[display_var_multiapp] = robustness_cf_dist('multiapp');
% Leaving out vacancies posted within week of teacher first applying
[display_var_teach_wkdonut] = robustness_cf_dist('teach_wkdonut');
[display_var_dropapp] = robustness_cf_dist('drop_app');


% part 3: Estimate principal preferences using rank order logit: baseline is binary logit

[display_var_principal_ro_all] = robustness_cf_dist('principal_ro_all');
[display_var_principal_ro_restricted] = robustness_cf_dist('principal_ro_restricted');
[display_var_principal_hire] = robustness_cf_dist('principal_hire');

% part 4.   Vary window in which we estimate principal preferences: baseline is all applications

[display_var_principal_wi14] = robustness_cf_dist('principal_wi14');
[display_var_principal_first] = robustness_cf_dist('principal_first');
[display_var_principal_second] = robustness_cf_dist('principal_second');

% part 5: Vary student type split: baseline is economic disadvantage

[display_var_race] = robustness_cf_dist('race');
[display_var_ach] = robustness_cf_dist('ach');

% renormalize to have the same mean in status quo as baseline
display_var_race=display_var_race-display_var_race(1,1) + display_var_baseline(1,1);
display_var_ach=display_var_ach-display_var_ach(1,1) + display_var_baseline(1,1);

% part 6. Alternative value-added models

[display_var_homog] = robustness_cf_dist('homog');
[display_var_sM] = robustness_cf_dist('sM');
[display_var_CFR] = robustness_cf_dist('CFR');

% renormalize to have the same mean in status quo as baseline
display_var_homog=display_var_homog-display_var_homog(1,1) + display_var_baseline(1,1);
display_var_sM=display_var_sM-display_var_sM(1,1) + display_var_baseline(1,1);
display_var_CFR=display_var_CFR-display_var_CFR(1,1) + display_var_baseline(1,1);

% part 7.  Allow for correlated random coefficients in teacher preferences

[display_var_corrRC] = robustness_cf_dist('corrRC');

% part 8. Vary teacher preference specification to use binary logit

[display_var_logit1] = robustness_cf_dist('logit1');
[display_var_logit6] = robustness_cf_dist('logit6');
[display_var_logit7] = robustness_cf_dist('logit7');


% part 9 No VA in teacher preferences  
[display_var_teach_nova] = robustness_cf_dist('teach_nova');



%% 10. Multiply value-added coefficients by 10 in principal model

[display_var_attenuation] = robustness_cf_dist('attenuation');

%% 11. Efficiency

[display_var_efficiency] = robustness_cf_efficiency('efficiency');
% renormalize to have the same mean in status quo as baseline
display_var_efficiency=display_var_efficiency-display_var_efficiency(1,1) + display_var_baseline(1,1);

% 12. Imputed teachers

[balanced_baseline schoolshort_baseline] = robustness_imputation('imputed');

% renormalize to have the same mean in status quo as baseline
balanced_baseline=balanced_baseline-balanced_baseline(1,1) + display_var_baseline(1,1);
schoolshort_baseline=schoolshort_baseline-schoolshort_baseline(1,1) + display_var_baseline(1,1);

% 13. timing
[oneperiod_all oneperiod_pd1 oneperiod_pd2 oneperiod_pd3 thrperiod_all thrperiod_pd1 thrperiod_pd2 thrperiod_pd3   ] = robustness_timing('monthbymonth');


%%
% MATLAB script to produce a LaTeX table with dynamic data

% Specify the directory path where you want to save the .tex file
save_path = 'E:\ReplicateBuild\03_output\tables\';

% Complete file path with file name
file_name = [save_path 'robustness_table.tex'];

% Open a file to write
fileID = fopen(file_name,'w');

% Check if the file was opened successfully
if fileID == -1
    error('File could not be opened, check if the path is correct and writable.');
end

% Write the table header and begin the tabular environment
fprintf(fileID, '\\begin{tabular}{lcccccc}\\hline  \\noalign{\\smallskip} \\multicolumn{1}{c}{} & \\multicolumn{1}{l}{Status quo} & \\multicolumn{1}{l}{All options} & \\multicolumn{1}{l}{Principal} & \\multicolumn{1}{l}{Teach} & \\multicolumn{1}{l}{Previous} & \\multicolumn{1}{l}{First best} \\    & & & & Max VA & Max N Dis & Two \\  \\noalign{\\smallskip}  \\\\\\hline\n');

% Panel 0: Baseline
fprintf(fileID, 'Baseline & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_baseline);
%fprintf(fileID, 'Imputed & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', display_var_imputed);


% Panel 1: Constant class size
fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{1. Hold class sizes constant: baseline uses class size}} \\\\\n');
fprintf(fileID, 'Constant class size & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_constant);
fprintf(fileID, 'Constant class size (CFR) & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_CFRconstant);

% Panel 2: Vary choice set construction for teachers
fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{2. Vary choice set construction for teachers}} \\\\\n');
fprintf(fileID, '7 day buffer & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_teach_buff7);
fprintf(fileID, 'First day choice sets only & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_teach_day1);
fprintf(fileID, 'Drop single app. teachers & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_multiapp);
fprintf(fileID, 'Donut & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_teach_wkdonut);
fprintf(fileID, 'Drop 20 percent of apps. & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_dropapp);

% include the drop 20% of applications

% Panel 3: Estimate principal preferences using rank order logit: baseline is binary logit
fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{3. Estimate principal preferences using rank order logit: baseline is binary logit}} \\\\\n');
fprintf(fileID, 'All data & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_ro_all);
fprintf(fileID, 'Active choices & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_ro_restricted);
fprintf(fileID, 'Hire outcome only & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_hire);

% part 4.   Vary window in which we estimate principal preferences: baseline is all applications
fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{4. Vary window in which we estimate principal preferences: baseline is all applications}} \\\\\n');
fprintf(fileID, 'W/in 2 weeks of hire & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_wi14);
fprintf(fileID, 'First half & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_first);
fprintf(fileID, 'Second half & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_principal_second);

% part 5: Vary student type split: baseline is economic disadvantage

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{5. Vary student type split: baseline is economic disadvantage}} \\\\\n');
fprintf(fileID, 'Achievement & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_ach);
fprintf(fileID, 'Race & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_race);

% part 6. Alternative value-added models

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{6. Alternative value-added models}} \\\\\n');
fprintf(fileID, 'Homogenous & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_homog);
fprintf(fileID, 'Using school means & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_sM);
fprintf(fileID, 'CFR & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_CFR);
% Residual 

% part 7.  Allow for correlated random coefficients in teacher preferences
fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{7. Allow for correlated random coefficients in teacher preferences}} \\\\\n');
fprintf(fileID, 'Corr. R.C. & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_corrRC);

% part 8. Vary teacher preference specification to use binary logit

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{8. Vary teacher preference specification to use binary logit}} \\\\\n');
fprintf(fileID, 'No REs or FEs & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_logit1);
fprintf(fileID, 'Teacher REs, School FEs & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_logit6);
fprintf(fileID, 'Teacher FEs, School FEs & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_logit7);

% part 9.  Omit value-added from teacher preferences 

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{9. Omit value-added from teacher preferences} } \\\\\n');
%fprintf(fileID, 'Additional X & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', display_var_extraX);
fprintf(fileID, 'No VA & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_teach_nova);


% 10. Multiply value-added coefficients by 10 in principal model

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{10. Multiply value-added coefficients by 10 in principal model}} \\\\\n');
fprintf(fileID, 'Multiply by 10 & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_attenuation);


% 11. Efficiency objective: outcome is mean achievement

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{11. Efficiency objective: outcome is mean achievement}} \\\\\n');
fprintf(fileID, ' & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', display_var_efficiency);



% 12. Impute value-added for teachers without value added 

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{12. Impute value-added for teachers without value added}} \\\\\n');
fprintf(fileID, 'Balanced & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', balanced_baseline);
fprintf(fileID, 'Teachers long & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', schoolshort_baseline);
%fprintf(fileID, 'Shrunk, balanced & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', balanced_shr);
%fprintf(fileID, 'Shrunk, teachers long & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', schoolshort_shr);
%fprintf(fileID, '3rd version, balanced & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', balanced_alt);
%fprintf(fileID, '3rd version, teachers long & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\n', schoolshort_alt);

% 13. The role of timing 

fprintf(fileID, '\\hline \\multicolumn{7}{l}{\\textit{13. The role of timing}} \\\\\n');
fprintf(fileID, 'One period, period 1 & %.3f & N/A & N/A & N/A & N/A & N/A \\\\\n', oneperiod_pd1(1,1));
fprintf(fileID, 'One period, period 2 & %.3f & N/A & N/A & N/A & N/A & N/A \\\\\n', oneperiod_pd2(1,1));
fprintf(fileID, 'One period, period 3 & %.3f & N/A & N/A & N/A & N/A & N/A \\\\\n', oneperiod_pd3(1,1));
fprintf(fileID, 'Three periods, overall & %.3f & N/A & N/A & N/A & N/A & N/A \\\\\n', thrperiod_all(1,1));
%fprintf(fileID, 'Three periods, period 1 & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', thrperiod_pd1);
%fprintf(fileID, 'Three periods, period 2 & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', thrperiod_pd2);
%fprintf(fileID, 'Three periods, period 3 & %.3f & %.3f & %.3f & %.3f & %.3f & %.3f \\\\\\hline\n', thrperiod_pd3);



% End the tabular environment and close the table
fprintf(fileID, '  \\noalign{\\smallskip}\\hline \\end{tabular}\n');

% Close the file
fclose(fileID);

% Notify the user
fprintf('LaTeX table saved to %s\n', file_name);

